package com.acxiom.foodreview.controller.api;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.acxiom.foodreview.entity.Examples;
import com.acxiom.foodreview.entity.Result;
import com.acxiom.foodreview.service.ExamplesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Created by hadong on 2017-03-23. ExamplesController
 */
@Api(value="接口示例")
@RestController
public class ExamplesController extends ApiController {

	@Autowired
	private ExamplesService examplesService;

	private Logger log = Logger.getLogger(ExamplesController.class);

	/**
	 * index
	 * 
	 * @param examples
	 * @return
	 */
	@RequestMapping("examples/index")
	@ApiOperation(notes = "首页", value = "首页", httpMethod = "GET")
	public String index() {
		return "index";
	}

	/**
	 * insert
	 * 
	 * @param examples
	 * @return
	 */
	@RequestMapping("examples/insert")
	@ApiOperation(notes = "添加示例", value = "添加示例", httpMethod = "POST")
	@ApiImplicitParam(name = "examples", value = "示例实体", required = true, dataType = "Examples")
	public Result insert(@RequestBody Examples examples) {
		if (examplesService.insert(examples)) {
			return new Result();
		}
		return new Result(500, "insert failed");
	}

	/**
	 * findbyid
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("examples/findbyid")
	@ApiOperation(notes = "查询示例(两层结构)", value = "查询示例(两层结构)", httpMethod = "POST")
	@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "int")
	public Result findbyid(@RequestParam Integer id) {
		log.info("select2:@--@2");
		return new Result(examplesService.findById(id));
	}

	/**
	 * findaspage
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("examples/findaspage")
	@ApiOperation(notes = "查询示例(分页)", value = "查询示例(分页)", httpMethod = "POST")
	@ApiImplicitParams(value = { @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "rows", value = "每页记录数", required = true, paramType = "query", dataType = "int") })
	public Result findaspage(@RequestParam Integer page, @RequestParam Integer rows) {
		log.info("select3:@--@3");
		return new Result(examplesService.findAsPage(page, rows));
	}

	/**
	 * update
	 * 
	 * @param examples
	 * @return
	 */
	@RequestMapping("examples/update")
	@ApiOperation(notes = "更新示例", value = "更新示例", httpMethod = "POST")
	@ApiImplicitParam(name = "examples", value = "示例实体", required = true, dataType = "Examples")
	public Result update(@RequestBody Examples examples) {
		if (examplesService.update(examples)) {
			return new Result();
		}
		return new Result(500, "update failed");
	}

	/**
	 * delete
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("examples/delete")
	@ApiOperation(notes = "删除示例", value = "删除示例", httpMethod = "POST")
	@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "int")
	public Result delete(@RequestParam Integer id) {
		if (examplesService.delete(id)) {
			return new Result();
		}
		return new Result(500, "delete failed");
	}
}
